package org.example.teachermanager.api;

import org.example.teachermanager.entity.Teacher;
import org.example.teachermanager.service.TeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/teacher")
public class TeacherApiController {
    @Autowired
    private TeacherService teacherService;
    @GetMapping("/list")
    public Page<Teacher> getAllTeachers(@RequestParam(defaultValue = "0") int page,
                                        @RequestParam(defaultValue = "12") int size) {
        Pageable pageable = PageRequest.of(page, size);
        return teacherService.findAll(pageable);
    }

    @GetMapping("/findByNameLike")
    public Page<Teacher> findTeacherByNameLike(@RequestParam String name,
                                               @RequestParam(defaultValue = "0") int page,
                                               @RequestParam(defaultValue = "12") int size) {
        Pageable pageable = PageRequest.of(page, size);
        return teacherService.findByNameLike(name, pageable);
    }
    @DeleteMapping("/delete")
    public void deleteTeacher(@RequestParam int id) {
        teacherService.delete(id);
    }
    @PutMapping("/update")
    public void updateTeacher(@RequestBody Teacher teacher) {
        teacherService.update(teacher);
    }
    @PostMapping("/add")
    public Teacher addTeacher( Teacher teacher) {
       return teacherService.add(teacher);
    }
    @GetMapping("/findBySex")
    public Page<Teacher> findTeacherBySexLike(@RequestParam String sex,
                                              @RequestParam(defaultValue = "0") int page,
                                              @RequestParam(defaultValue = "12") int size) {
        Pageable pageable = PageRequest.of(page, size);
        return teacherService.findBySex(sex, pageable);
    }

    @GetMapping("/findByNumber")
    public Page<Teacher> findTeacherByNumberLike(@RequestParam String number,
                                                 @RequestParam(defaultValue = "0") int page,
                                                 @RequestParam(defaultValue = "12") int size) {
        Pageable pageable = PageRequest.of(page, size);
        return teacherService.findByNumber(number, pageable);
    }
}
